package com.smartao.base.role;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.smartao.base.Constant;
import com.smartao.base.employee.Employee;
import com.smartao.base.employee.EmployeeService;
import com.smartao.base.selectitem.Selectitem;
import com.smartao.base.selectitem.SelectitemService;
import com.smartao.core.mvc.BaseAction;

@Controller
@Scope("prototype")
@Results({@Result(name = "jsonSuccess", type="json", params={"root","jsonMap"}),
		@Result(name = "jsonFailure", type="json", params={"root","jsonMap"}),
		@Result(name = "gotoList", type="redirect", location="role.action"),
		@Result(name = "updateAccount", location="account.jsp"),
		@Result(name = "listUser", location="listUserByRole.jsp"),
		@Result(name = "addUserToRole", location="addUserToRole.jsp"),
		@Result(name = "update", location="update.jsp")})
public class RoleAction extends BaseAction{

	private static final long serialVersionUID = -7840660758352652783L;

	@Autowired
	RoleService roleService;
	
	@Autowired
	private EmployeeService employeeService;
	
	@Autowired
	private SelectitemService selectitemService;
	
	// 当前操作的角色
	private Role role;
	// 当前操作的角色列表；
	private List<Role> roles;
	// 角色类型列表；
	private List<Selectitem> roleTypes;
	// 当前操作的角色类型；
	private Selectitem roleType;
	// 当前传入的角色类型Id；
	private Long roleTypeId;
	
	private Long employeeId;
	
	private Long[] employeeIds;
	
	private Collection<Employee> employees;
	
	private String message;
	
	@Override
	public String execute(){
		prepareRoleType();
		roles = roleService.findByRoleType(roleTypeId);
		return SUCCESS;
	}
	
	public String preUpdate(){
		prepareRoleType();
		// 更新
		if(id!=null){
			role = roleService.findOne(id);
		}
		// 新增
		else{
			role = new Role();
			role.setRoleType(roleType);
		}
		return "update";
	}
	
	public String update(){
		if(role.getId()!=null){
			// 此处异常奇怪；因为role和user多对多， 所以，修改保存role的时候需要重设users；
			Role r = roleService.findOne(role.getId());
			// 于是我手动重新取了users；问题就产生了，实际上我通过debug看到
			// findOne方法并没有发users取出来；
			role.setUsers(r.getUsers());
			// 而下面的save居然能够给被修改的role保持住users;但是如果把上面这句话去掉就不行了；
		}
		roleService.save(role);
		prepareRoleType();
		message = "保存成功.";
		return "update";
	}
	
	public String delete(){
		// TODO: 删除role需做更多考虑; 
		// 此处删除role会同时删除employee_role关系型；
		// 那么其他涉及到和role关联的地方该如何考虑；
		// 比如被流程关联；
		roleService.delete(id);
		return "gotoList";
	}
	
	public String listUserByRole(){
		role = roleService.findRoleAndUser(id);
		employees = role.getUsers();
		return "listUser";
	}
	
	public String preAddUserToRole(){
		role = roleService.findOne(id);
		//角色上添加用户
		//employees = employeeService.findAll();
		return "addUserToRole";
	}
	
	public String addUserToRole(){
		role = roleService.findRoleAndUser(id);
		List<Employee> users = employeeService.findByIds(employeeIds);
		for(Employee em : users){
			role.addUser(em);
		}
		roleService.save(role);
		employees = role.getUsers();
		return "listUser";
	}
	
	public String deleteUserFromRole(){
		role = roleService.findRoleAndUser(id);
		for(Employee em : role.getUsers()){
			if(em.getId().equals(employeeId)){
				role.getUsers().remove(em);
				break;
			}
		}
		roleService.save(role);
		employees = role.getUsers();
		return "listUser";
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<Selectitem> getRoleTypes() {
		return roleTypes;
	}

	public void setRoleTypes(List<Selectitem> roleTypes) {
		this.roleTypes = roleTypes;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Selectitem getRoleType() {
		return roleType;
	}

	public void setRoleType(Selectitem roleType) {
		this.roleType = roleType;
	}

	public Long getRoleTypeId() {
		return roleTypeId;
	}

	public void setRoleTypeId(Long roleTypeId) {
		this.roleTypeId = roleTypeId;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Collection<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(Collection<Employee> employees) {
		this.employees = employees;
	}

	public Long getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(Long employeeId) {
		this.employeeId = employeeId;
	}

	public Long[] getEmployeeIds() {
		return employeeIds;
	}

	public void setEmployeeIds(Long[] employeeIds) {
		this.employeeIds = employeeIds;
	}

	private void prepareRoleType(){
		roleTypes = selectitemService.findBySelecttypeCode(Constant.SELECTTYPE_ROLE_TYPE);
		// 如果前端没有传入角色类型， 则使用系统角色类型；
		if(roleTypeId==null){
			for(Selectitem item : roleTypes){
				if(item.getCode().equals(Constant.SELECTITEM_ROLE_TYPE_SYSTEM)){
					roleType = item;
					roleTypeId = roleType.getId();
					break;
				}
			}
		}
		// 否则使用传入的角色类型；
		else{
			for(Selectitem item : roleTypes){
				if(item.getId().equals(roleTypeId)){
					roleType = item;
					break;
				}
			}
		}
	}
	
}
